//
//  GMNotesViewCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/6/28.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMNotesViewCell.h"
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>

#import "XZNotesModel.h"
#import "GMAttachListModel.h"
#import "GMVoiceListModel.h"
@interface GMNotesViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>
//时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
//内容
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
//语音
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//图片滚动视图
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
//图片
@property (weak, nonatomic) IBOutlet UIImageView *attachementImg;
//AttachementResname
@property (weak, nonatomic) IBOutlet UIButton *resNameLabel;
//Attachement->positionStr
@property (weak, nonatomic) IBOutlet UIButton *positionStrBtn;
@property (weak, nonatomic) IBOutlet UIView *attachementView;

/************NSLayoutConstraint*********/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bodyViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *attachemnetHeight;

@end

@implementation GMNotesViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    //TableView的属性设置
    _tableView.delegate =self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = false;
    [_tableView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    //Collectionview的属性设置
    _collectionView.scrollEnabled = false;
    _collectionView.backgroundColor = kWhiteColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
}
- (void)setModel:(XZNotesModel *)model{
    _model = model;
    
    self.timeLabel.text = model.Time;
    self.contentLabel.text = model.Content;
    CGFloat contentHeigt = [self.contentLabel getLableSizeWithMaxWidth:(DEVICE_WIDTH-18)].height;
    //语音
    if (model.VoiceList.count == 0) {
        self.tableViewHeight.constant = 0;
        self.tableView.hidden = true;
    }else{
        self.tableViewHeight.constant = model.VoiceList.count *30;
        self.tableView.hidden = false;
        [self.tableView reloadData];
    }
    //图片
    if (model.ImgFileList.count == 0) {
        self.collectionViewHeight.constant = 0;
        self.collectionView.hidden = true;
    }else{
        self.collectionViewHeight.constant = 60;
        self.collectionView.hidden = false;
        [self.collectionView reloadData];
    }
    
    if (model.Attachment != nil) {
        self.attachementView.hidden = false;
        self.attachementImg.hidden = false;
        self.positionStrBtn.hidden = false;
        self.resNameLabel.hidden = false;
        self.attachemnetHeight.constant = 30;
    }else{
        self.attachementView.hidden = true;
        self.attachementImg.hidden = true;
        self.positionStrBtn.hidden = true;
        self.resNameLabel.hidden = true;
        self.attachemnetHeight.constant = 0;
    }
    //附加信息
    [self.attachementImg yy_setImageWithURL:[NSURL URLWithString:model.Attachment.HeadUrl] placeholder:DefealtImg];
    [self.resNameLabel setTitle:[NSString stringWithFormat:@"%@",model.Attachment.ResName] forState:UIControlStateNormal];
    [self.positionStrBtn setTitle:[NSString stringWithFormat:@"【%@】",model.Attachment.PositionStr] forState:UIControlStateNormal];
    NSInteger resType = [model.Attachment.ResType integerValue];
    self.resNameLabel.userInteractionEnabled = true;
    if (resType > 2) {
        self.resNameLabel.userInteractionEnabled = false;
        [self.resNameLabel setTitleColor:kBlackColor forState:UIControlStateNormal];
    }
    //Attachment.ResType=1&&Attachment.PositionStr!=null时显示
    if ([model.Attachment.ResType integerValue] == 1 && model.Attachment.PositionStr != nil) {
        self.positionStrBtn.hidden = false;
    }else{
        self.positionStrBtn.hidden = true;;
    }
    self.bodyViewHeight.constant = 32 + contentHeigt + self.tableViewHeight.constant+self.collectionViewHeight.constant+self.attachemnetHeight.constant + 20;
    model.cellHeight = self.bodyViewHeight.constant;
}

#pragma mark - CollectViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.ImgFileList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
    GMAttachListModel *model = self.model.ImgFileList[indexPath.row];
    [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
    cell.delectBtn.hidden=YES;
    return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    /**放大图片*/
    GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
    NSMutableArray *items = @[].mutableCopy;
    for (int i = 0; i < self.model.ImgFileList.count; i++){
        GMAttachListModel *model= self.model.ImgFileList[i];
        UIImageView *imgV = cell.photoImg;
        KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
        [items addObject:item];
    }
    KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
    browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
    browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
    browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
    browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
    browser.bounces = NO;
    [browser showFromViewController:self.parentController];
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.VoiceList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakObject(self)
    GMVoiceListModel *model = self.model.VoiceList[indexPath.row];
    GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = kWhiteColor;
    cell.model = model;
    //语音读与未读
    cell.readBlock = ^{
        weakself.hasReadBlock(indexPath.row, model.Id);
    };
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

//位置
- (IBAction)positionStrButtonAction:(UIButton *)sender {
    if (self.NotesBlock) {
        self.NotesBlock(self.model, @"POSITION");
    }
}
//跳转播放或者文档界面
- (IBAction)positionResNameButtonAction:(UIButton *)sender {
    if (self.NotesBlock) {
        self.NotesBlock(self.model, @"PLAYORDOCUMENT");
    }
}


@end
